package com.ruoyi.basebill.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsSumsaleissuebillentry;
import com.ruoyi.basebill.mapper.OmsSumsaleissuebillMapper;
import com.ruoyi.basebill.domain.OmsSumsaleissuebill;
import com.ruoyi.basebill.service.IOmsSumsaleissuebillService;

/**
 * 汇总销售出库单Service业务层处理
 * 
 * @author wenyixiong
 * @date 2023-02-18
 */
@Service
public class OmsSumsaleissuebillServiceImpl implements IOmsSumsaleissuebillService 
{
    @Autowired
    private OmsSumsaleissuebillMapper omsSumsaleissuebillMapper;
    @Autowired
    private OmsSumsaleissuebillServiceImpl sumsaleissuebillService;

    /**
     * 查询汇总销售出库单
     * 
     * @param id 汇总销售出库单主键
     * @return 汇总销售出库单
     */
    @Override
    public OmsSumsaleissuebill selectOmsSumsaleissuebillById(Long id)
    {
        return omsSumsaleissuebillMapper.selectOmsSumsaleissuebillById(id);
    }

    /**
     * 查询汇总销售出库单列表
     * 
     * @param omsSumsaleissuebill 汇总销售出库单
     * @return 汇总销售出库单
     */
    @Override
    public List<OmsSumsaleissuebill> selectOmsSumsaleissuebillList(OmsSumsaleissuebill omsSumsaleissuebill)
    {
        return omsSumsaleissuebillMapper.selectOmsSumsaleissuebillList(omsSumsaleissuebill);
    }


    public List<OmsSumsaleissuebill> selectOmsSumsaleissuebillDetailList(OmsSumsaleissuebill omsSumsaleissuebill){
        return omsSumsaleissuebillMapper.selectOmsSumsaleissuebillDetailList(omsSumsaleissuebill);
    }

    /**
     * 新增汇总销售出库单
     * 
     * @param omsSumsaleissuebill 汇总销售出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsSumsaleissuebill(OmsSumsaleissuebill omsSumsaleissuebill)
    {
        int rows = omsSumsaleissuebillMapper.insertOmsSumsaleissuebill(omsSumsaleissuebill);
        sumsaleissuebillService.insertOmsSumsaleissuebillentry(omsSumsaleissuebill);
        return rows;
    }

    /**
     * 修改汇总销售出库单
     * 
     * @param omsSumsaleissuebill 汇总销售出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsSumsaleissuebill(OmsSumsaleissuebill omsSumsaleissuebill)
    {
        //传入汇总销售出库单不存在分录则不用修改分录信息只修改表头信息
        if(omsSumsaleissuebill.getOmsSumsaleissuebillentryList() != null && omsSumsaleissuebill.getOmsSumsaleissuebillentryList().size() != 0) {
            omsSumsaleissuebillMapper.deleteOmsSumsaleissuebillentryByParentid(omsSumsaleissuebill.getId());
            sumsaleissuebillService.insertOmsSumsaleissuebillentry(omsSumsaleissuebill);
        }
        return omsSumsaleissuebillMapper.updateOmsSumsaleissuebill(omsSumsaleissuebill);
    }

    /**
     * 批量删除汇总销售出库单
     * 
     * @param ids 需要删除的汇总销售出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSumsaleissuebillByIds(Long[] ids)
    {
        omsSumsaleissuebillMapper.deleteOmsSumsaleissuebillentryByParentids(ids);
        return omsSumsaleissuebillMapper.deleteOmsSumsaleissuebillByIds(ids);
    }

    /**
     * 删除汇总销售出库单信息
     * 
     * @param id 汇总销售出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSumsaleissuebillById(Long id)
    {
        omsSumsaleissuebillMapper.deleteOmsSumsaleissuebillentryByParentid(id);
        return omsSumsaleissuebillMapper.deleteOmsSumsaleissuebillById(id);
    }

    /**
     * 新增汇总销售出库单明细信息
     * 
     * @param omsSumsaleissuebill 汇总销售出库单对象
     */
    public void insertOmsSumsaleissuebillentry(OmsSumsaleissuebill omsSumsaleissuebill)
    {
        List<OmsSumsaleissuebillentry> omsSumsaleissuebillentryList = omsSumsaleissuebill.getOmsSumsaleissuebillentryList();
        Long id = omsSumsaleissuebill.getId();
        if (StringUtils.isNotNull(omsSumsaleissuebillentryList))
        {
            List<OmsSumsaleissuebillentry> list = new ArrayList<OmsSumsaleissuebillentry>();
            for (OmsSumsaleissuebillentry omsSumsaleissuebillentry : omsSumsaleissuebillentryList)
            {
                omsSumsaleissuebillentry.setParentid(id);
                list.add(omsSumsaleissuebillentry);
            }
            if (list.size() > 0)
            {
                omsSumsaleissuebillMapper.batchOmsSumsaleissuebillentry(list);
            }
        }
    }

    /**
     * 查询是否推送过EAS的汇总销售出库单数据
     * @param isTrans
     * @return
     */
    public List<OmsSumsaleissuebill> selectPushEASSumSaleissuebillList(int isTrans){
        return omsSumsaleissuebillMapper.selectPushEASSumSaleissuebillList(isTrans);
    }
}
